Los SearchControls por defecto especifican que la b�squeda se realice en el contexto llamado (SearchControls.ONELEVEL_SCOPE).
Este valor por defecto se usa en los ejemplos de la secci�n de Filtros de B�squeda.
Adem�s de estos valores por defecto, podemos especificar que la b�squeda se realice en un sub�rbol entero o s�lo en el objeto nombrado.
�Buscar en el Sub�rbol
Una b�squeda en el sub�rbol completo busca el objeto nombrado y todos su descendientes. Para hacer que la b�squeda se comporte de esta forma, pasamos SearchControls.SUBTREE_SCOPE a SearchControls.setSearchScope() como sigue.
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Specify the search filter to match
// Ask for objects that have the attribute "sn" == "Geisel"
// and the "mail" attribute
String filter = "(&(sn=Geisel)(mail=*))";
// Search the subtree for objects by using the filter
NamingEnumeration answer = ctx.search("", filter, ctls);
Este ejemplo busca en el sub�rbol del contexto ctx todas las entradas que cumplan el filtro de b�squeda. Encuentra la entrada "cn= Ted Geisel, ou=People" en este sub�rbol que cumple con el filtro.
# java SearchSubtree >>>cn=Ted Geisel, ou=People attribute: sn value: Geisel attribute: mail value: [email protected] attribute: telephonenumber value: +1 408 555 5252
�Buscar el Objeto Nombrado
Tambi�n podemos buscar el objeto nombrado. Esto es �til, por ejemplo, para comprobar si el objeto nombrado cumple las condiciones de b�squeda. Para buscar el objeto nombrado le pasamos SearchControls.OBJECT_SCOPE a setSearchScope().
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
// Specify the search filter to match
// Ask for objects that have the attribute "sn" == "Geisel"
// and the "mail" attribute
String filter = "(&(sn=Geisel)(mail=*))";
// Search the subtree for objects by using the filter
NamingEnumeration answer =
ctx.search("cn=Ted Geisel, ou=People", filter, ctls);
Este ejemplo comprueba que el objeto "cn=Ted Geisel, ou=People" cumple el filtro dado.
# java SearchObject >>> attribute: sn value: Geisel attribute: mail value: [email protected] attribute: telephonenumber value: +1 408 555 5252
El ejemplo econtr� una respuesta y la imprimi�. Observa que el nombre del resultado es una cadena vac�a. Esto es porque el nombre del objeto es siempre relativo al contexto de b�squeda (en este caso "cn=Ted Geisel, ou=People").